﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Atom.dao.Model;
using Atom.NetFrame;
using Atom.Tool;
using Protocol;
using Atom.biz;
using Protocol.dto;

namespace Atom.Logic.User
{
    public class UserHandler : AbsOnceHandler, HandleInterface
    {
        public void ClientClose(UserToken ut, string error)
        {
            UserBiz.OffLine(ut);
        }

        public void ClientConnect(UserToken ut)
        {

        }

        public void MessageReceive(UserToken ut, SocketModel message)
        {
            switch (message.Command)
            {
                case UserProtocol.INFO_REQ:
                    Info(ut);
                    break;
                case UserProtocol.CREATE_REQ:
                    Create(ut, ReqCreateUser.Decode(message.Message).UserName);
                    break;
                case UserProtocol.ONLINE_REQ:
                    OnLine(ut);
                    break;
            }
        }

        private void Create(UserToken ut, string name)
        {
            ExecutorPool.Instance.Executor(
                delegate
                {
                    bool isOK = UserBiz.Create(ut, name);
                    int result = isOK ? 0 : 1;
                    ResCreateUser res = new ResCreateUser();
                    res.Result = result;
                    SendMsg(ut, UserProtocol.CREATE_RES, res.Encode());

                }
            );
        }


        private void Info(UserToken ut)
        {
            ExecutorPool.Instance.Executor(
                delegate
                {
                    UserMode user = UserBiz.GetUserOnGetInfo(ut);
                    //Write(ut, UserProtocol.INFFO_SERS, Converter(user));
                }
            );
        }

        private void OnLine(UserToken ut)
        {
            ExecutorPool.Instance.Executor(
                delegate
                {
                    UserMode user = UserBiz.OnLine(ut);
                    ResOnline online = new ResOnline();
                    if (user != null)
                    {
                        online.HasUser = true;
                        online.ID = user.ID;
                        online.AccountID = user.AccountID;
                        online.Name = user.Name;
                        online.Level = user.Level;
                        online.Exp = user.Exp;
                        online.WinCount = user.WinCount;
                        online.LoseCount = user.LoseCount;
                        online.RunCount = user.RunCount;
                    }
                    else
                    {
                        online.HasUser = false;
                    }
                    SendMsg(ut, UserProtocol.ONLINE_RES, online.Encode());
                }
            );
        }

        private UserDTO Converter(UserMode user)
        {
            if (user == null) return null;
            UserDTO dto = new UserDTO(user.ID, user.AccountID, user.Name, user.Level, user.Exp, user.WinCount, user.LoseCount, user.RunCount, user.HeroList.ToArray());
            return dto;
        }

        public override int GetArea()
        {
            return 0;
        }

        public override byte Gettype()
        {
            return GameProtocol.TYPE_USER;
        }
    }
}
